﻿namespace OpenComposite.Repository.Forms.TrackingMonitor
{
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Data.SqlClient;
	using System.Drawing;
	using System.Globalization;
	using System.Linq;
	using System.Text;
	using System.Windows.Forms;
	using System.Workflow.Runtime.Tracking;

	using OpenComposite.Base;
	using OpenComposite.Base.Collections;

	using WeifenLuo.WinFormsUI.Docking;
	using OpenComposite.Repository.Tracking;

	public partial class TrackedProcessList : DockContent
	{
		#region Initialization

		public TrackedProcessList()
		{
			InitializeComponent();
			dgvWfInstances.AutoGenerateColumns = false;
			//colProcessStart.DefaultCellStyle.Format = Helper.ExactDateTimeFormat;
			//colProcessEnd.DefaultCellStyle.Format = Helper.ExactDateTimeFormat;
		}

		#endregion Initialization

		#region Internal Members

		#region Events

		internal event EventHandler<EventArgs<TrackedProcessInstance>> ShowWorkflow;

		#endregion Events

		#region Properties

		internal DataGridView WorkflowInstances
		{
			get { return dgvWfInstances; }
		}

		#endregion Properties

		#endregion Internal Members

		#region Private Members

		#region Event Handlers

		private void dgvWfInstances_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
		{
			if ( e.RowIndex < 0 ) return;
			if ( e.ColumnIndex == colProcessStart.Index || e.ColumnIndex == colProcessEnd.Index ) {
				DateTime value = (DateTime)e.Value;
				e.Value = value.ToLocalTime().ToString("g");
				e.FormattingApplied = true;
			}
		}

		private void dgvWfInstances_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
		{
			if ( e.RowIndex < 0 ) return;
			TrackedProcessInstance instance = dgvWfInstances.Rows[e.RowIndex].DataBoundItem as TrackedProcessInstance;
			if ( instance == null ) return;
			e.ToolTipText = instance.ToolTip;
		}

		private void dgvWfInstances_RowEnter(object sender, DataGridViewCellEventArgs e)
		{
			if ( e.RowIndex < 0 ) return;
			TrackedProcessInstance item = dgvWfInstances.Rows[e.RowIndex].DataBoundItem as TrackedProcessInstance;
			if ( item == null ) return;
			showWorkflow(item);
		}

		#endregion Event Handlers

		#region Methods

		private void showWorkflow(TrackedProcessInstance sqlTrackingWorkflowInstance)
		{
			EventHandler<EventArgs<TrackedProcessInstance>> tmp = ShowWorkflow;
			if ( tmp != null ) {
				tmp(this, new EventArgs<TrackedProcessInstance>(sqlTrackingWorkflowInstance));
			}
		}

		#endregion Methods

		#endregion Private Members
	}
}